package com.iol.stpso.util;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatHelper {
	
	private static final String default_datetime_fmt = Constants.DEFAULT_DATE_TIME_FMT;
	private static final String default_date_fmt = Constants.DEFAULT_DATE_FMT;
	private static final String default_date_minute_fmt = Constants.DEFAULT_DATE_MINUTE_FMT;
	private static final String default_minute_fmt = Constants.DEFAULT_MINUTE_FMT;
	private static final String default_time_fmt = Constants.DEFAULT_TIME_FMT;
	private static final String default_mstime_fmt = Constants.DEFAULT_MSTIME_FMT;
	private static final String default_morning = Constants.DEFAULT_MORING;
	private static final String default_afternoon = Constants.DEFAULT_AFTERNOON;
	private static final String week_sunday = Constants.WEEK_SUNDAY;
	private static final String week_monday = Constants.WEEK_MONDAY;
	private static final String week_tuseday = Constants.WEEK_TUSEDAY;
	private static final String week_wednesday = Constants.WEEK_WEDNESDAY;
	private static final String week_thursday = Constants.WEEK_THURSDAY;
	private static final String week_friday = Constants.WEEK_FRIDAY;
	private static final String week_saturday = Constants.WEEK_SATURDAY;
	
	/**
	 * datetimeToString
	 * @param date Date;
	 * @return String date; yyyy-MM-dd HH:mm:ss
	 */
	public static String datetimeToString(Date date){
		return convertToString(date, default_datetime_fmt);
	}
	
	/**
	 * dateToString
	 * @param date Date
	 * @return String date yyyy-MM-dd
	 */
	public static String dateToString (Date date){
		return convertToString (date, default_date_fmt);
	}
	
	/**
	 * String day-minuteToString
	 * @param date Date;
	 * @return String date; yyyy-MM-dd HH:mm
	 */
	public static String dateminuteToString (Date date){
		return convertToString (date, default_date_minute_fmt);
	}
	
	/**
	 * minuteToString
	 * @param date Date
	 * @return String date HH:mm
	 */
	public static String minuteToString(Date date) {
		return convertToString(date, default_minute_fmt);
	}
	
	/**
	 * timeToString
	 * @param date Date
	 * @return String date HH:mm:ss
	 */
	public static String timeToString(Date date) {
		return convertToString(date, default_time_fmt);
	}
	
	public static long yearBegin(Date date) {
		String year = convertToString(date, "yyyy");
		Date y = StringToDate(year + "-01-01 00:00:00.000", default_date_fmt
				+ "" + default_mstime_fmt);
		if (y != null) {
			return y.getTime();
		}
		return -1;
	}
	
	public static long yearEnd(Date date) {
		String year = convertToString(date, "yyyy");
		Date y = StringToDate(year + "-12-31 23:59:59.999", default_date_fmt
				+ "" + default_mstime_fmt);
		if (y != null) {
			return y.getTime();
		}
		return -1;
	}
	
	public static long monthBegin(Date date) {
		String month = convertToString(date, "yyyy-MM");
		Date m = StringToDate(month + "-01 00:00:00.000", default_date_fmt + ""
				+ default_mstime_fmt);
		if (m != null) {
			return m.getTime();
		}
		return -1;
	}
	
	public static long monthEnd(Date date) {
		int year = Year(date);
		int month = Month(date);
		if (month == 12) {
			year += 1;
			month = 1;
		} else {
			month += 1;
		}
		Date m = StringToDate(
				String.valueOf(year) + "-" + Utils.zeroFill(month, 2)
						+ "-01 00:00:00.000", default_date_fmt + ""
						+ default_mstime_fmt);
		if (m != null) {
			return m.getTime()-1;
		}
		return -1;
	}
	
	public static String timeTohalf(Date date) {
		String gz = "";
		if (null == date) {
			return gz;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("a", Locale.US);
		String timeTohalf = sdf.format(date);
		if (timeTohalf.equals(default_morning)) {
			gz = "上午";
		} else if (timeTohalf.equals(default_afternoon)) {
			gz = "下午";
		}
		return gz;
	}
	
	public static int Month(Date date) {
		return convertToInt(date, "MM");

	}

	public static int Day(Date date) {
		return convertToInt(date, "dd");
	}

	public static int Week(Date date) {
		return convertToInt(date, "ww");
	}
	
	public static int Hour(Date date) {
		return convertToInt(date, "HH");
	}

	public static int Minute(Date date) {
		return convertToInt(date, "mm");
	}

	public static int Second(Date date) {
		return convertToInt(date, "ss");
	}
	
	public static long weekBegin(Date date) {
		return weekBegin(date, true);
	}

	public static long weekBegin(Date date, boolean chineseLunar) {
		int firstday = Week(date, chineseLunar);
		if (chineseLunar) {
			firstday -= 1;
		}
		String day = convertToString(date, default_date_fmt);
		Date week = StringToDate(day + " 00:00:00.000", default_date_fmt + ""
				+ default_mstime_fmt);
		week = beforeDays(week, firstday);
		if (week != null) {
			return week.getTime();
		}
		return -1;
	}
    //前天
	public static Date beforeDays(Date date, int prede) {
		if (prede == 0)
			return date;
		if (date == null) {
			return null;
		}
		return new Date(date.getTime() - (24 * 60 * 60 * 1000 * prede));
	}

	public static long weekEnd(Date date) {
		return weekEnd(date, true);
	}

	public static long weekEnd(Date date, boolean chineseLunar) {
		int sevenday = Week(date, chineseLunar);
		if (chineseLunar) {
			sevenday = 7 - sevenday;
		} else {
			sevenday = 6 - sevenday;
		}
		String day = convertToString(date, default_date_fmt);
		Date week = StringToDate(day + " 23:59:59.999", default_date_fmt + ""
				+ default_mstime_fmt);
		week = afterDays(week, sevenday);
		if (week != null) {
			return week.getTime();
		}
		return -1;
	}
    //后天
	public static Date afterDays(Date date, int prede) {
		if (prede == 0)
			return date;
		if (date == null) {
			return null;
		}
		return new Date(date.getTime() + (24 * 60 * 60 * 1000 * prede));
	}

	private static int Week(Date date, boolean chineseLunar) {
		int zhou = -1;
		if (date == null) {
			return zhou;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("EEE", Locale.US);
		String Week = sdf.format(date);
		if (week_sunday.equals(Week)) {
			if (chineseLunar)
				zhou = 7;
			else
				zhou = 0;
		} else if (week_monday.equals(Week)) {
			zhou = 1;
		} else if (week_tuseday.equals(Week)) {
			zhou = 2;
		} else if (week_wednesday.equals(Week)) {
			zhou = 3;
		} else if (week_thursday.equals(Week)) {
			zhou = 4;
		} else if (week_friday.equals(Week)) {
			zhou = 5;
		} else if (week_saturday.equals(Week)) {
			zhou = 6;
		}
		return zhou;
	}

	private static int convertToInt(Date date, String fmt) {
		int nut = -1;
		String dateStr = convertToString(date, fmt);
		try {
			nut = Integer.parseInt(dateStr);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return nut;
	}

	private static int Year(Date date) {
		return Year(date,false);
	}

	private static int Year(Date date, boolean b) {
		if(b){
			return convertToInt(date,"yy");
		}
		return convertToInt(date,"yyyy");
	}

	@SuppressLint("SimpleDateFormat")
	public static Date StringToDate(String date, String fmt) {
		if(date == null||date.equals("")){
		return null;
		}
		try{
			SimpleDateFormat sdf = new SimpleDateFormat(fmt);
			return sdf.parse(date);
			}catch (ParseException e) {
				e.printStackTrace();
				}
		return null;		
	}

	@SuppressLint("SimpleDateFormat")
	public static String convertToString(Date date, String fmt) {
		if (date ==null){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(date);
	}
}
